import { defineStore } from 'pinia'
import { routerStore } from './router'
import { ITab } from './type/index'

export type TabsState = {
    tabsList: Array<ITab>
}

export const tabStore = defineStore('tabStore', {
    state: (): TabsState => {
        return {
            tabsList: []
        }
    },
    actions: {
        addTab(tab: ITab) {
            // 判断是否已经存在，如果不存在，才放入
            if (this.tabsList.some((item) => item.path === tab.path)) return

            this.tabsList.push(tab)
        },
        delTab(path: string, name: string) {
            const rStore = routerStore()
            // 重新设置选项卡数据
            this.tabsList = this.tabsList.filter(
                (tab: ITab) => tab.path !== path
            )
            rStore.delKeepAliveRouters(name)
        }
    },
    getters: {
        getTabs(state) {
            return state.tabsList
        }
    }
})
